package com.dt.dubai.search.controller;

import com.dt.dubai.core.comm.PageData;
import com.dt.dubai.core.exception.PageErrorMesg;
import com.dt.dubai.core.utile.I18nUtile;
import com.dt.dubai.search.entity.bean.EsProductBean;
import com.dt.dubai.search.entity.bean.EsProductRelatedInfoBean;
import com.dt.dubai.search.service.ser.EsProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 搜索商品管理
 */
@Controller
@RequestMapping("/esProduct")
public class EsProductController {
    @Autowired
    private EsProductService esProductService;

    @Autowired
    protected I18nUtile i18nUtile;

    /**
     * @return
     * 导入所有数据库中商品到ES
     */
    @RequestMapping(value = "/importAll", method = RequestMethod.POST)
    @ResponseBody
    public PageData<String> importAllList() {
        int count = esProductService.importAll();
        return new PageData().success(count, i18nUtile.getMessage(PageErrorMesg.E0.name()));
    }


    /**
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    @ResponseBody
    public PageData<String> delete(@PathVariable Long id) {
        esProductService.delete(id);
        return new PageData().success("", i18nUtile.getMessage(PageErrorMesg.E0.name()));
    }

    //根据id批量删除商品
    @RequestMapping(value = "/delete/batch", method = RequestMethod.POST)
    @ResponseBody
    public Object delete(@RequestParam("ids") List<Long> ids) {
        esProductService.delete(ids);
        return null;
    }

    //根据id创建商品
    @RequestMapping(value = "/create/{id}", method = RequestMethod.POST)
    @ResponseBody
    public PageData<String> create(@PathVariable Long id) {
        EsProductBean esProductBean = esProductService.create(id);
        if (esProductBean != null) {
            return new PageData().success(esProductBean, i18nUtile.getMessage(PageErrorMesg.E0.name()));
        } else {

            return null;
        }
    }

    //简单搜索
    @RequestMapping(value = "/search/simple", method = RequestMethod.GET)
    @ResponseBody
    public PageData<String> search(@RequestParam(required = false) String keyword,
                                                      @RequestParam(required = false, defaultValue = "0") Integer pageNum,
                                                      @RequestParam(required = false, defaultValue = "5") Integer pageSize) {
        Page<EsProductBean> esProductPage = esProductService.search(keyword, pageNum, pageSize);
        return new PageData().success(esProductPage, i18nUtile.getMessage(PageErrorMesg.E0.name()));
    }

   /* @ApiOperation(value = "综合搜索、筛选、排序")
    @ApiImplicitParam(name = "sort", value = "排序字段:0->按相关度；1->按新品；2->按销量；3->价格从低到高；4->价格从高到低",
            defaultValue = "0", allowableValues = "0,1,2,3,4", paramType = "query", dataType = "integer")*/

    @RequestMapping(value = "/search", method = RequestMethod.GET)
    @ResponseBody
    public PageData<EsProductBean> search(@RequestParam(required = false) String keyword,
                                          @RequestParam(required = false) Long brandId,
                                          @RequestParam(required = false) Long productCategoryId,
                                          @RequestParam(required = false, defaultValue = "0") Integer pageNum,
                                          @RequestParam(required = false, defaultValue = "5") Integer pageSize,
                                          @RequestParam(required = false, defaultValue = "0") Integer sort) {
        Page<EsProductBean> esProductPage = esProductService.search(keyword, brandId, productCategoryId, pageNum, pageSize, sort);
        return new PageData().success(esProductPage, i18nUtile.getMessage(PageErrorMesg.E0.name()));
    }

    /*@ApiOperation(value = "根据商品id推荐商品")*/
    @RequestMapping(value = "/recommend/{id}", method = RequestMethod.GET)
    @ResponseBody
    public PageData<String> recommend(@PathVariable Long id,
                                                         @RequestParam(required = false, defaultValue = "0") Integer pageNum,
                                                         @RequestParam(required = false, defaultValue = "5") Integer pageSize) {
        Page<EsProductBean> esProductPage = esProductService.recommend(id, pageNum, pageSize);
        return new PageData().success(esProductPage, i18nUtile.getMessage(PageErrorMesg.E0.name()));
    }

    /*@ApiOperation(value = "获取搜索的相关品牌、分类及筛选属性")*/
    @RequestMapping(value = "/search/relate", method = RequestMethod.GET)
    @ResponseBody
    public PageData<EsProductRelatedInfoBean> searchRelatedInfo(@RequestParam(required = false) String keyword) {
        EsProductRelatedInfoBean productRelatedInfo = esProductService.searchRelatedInfo(keyword);
        return new PageData().success(productRelatedInfo, i18nUtile.getMessage(PageErrorMesg.E0.name()));
    }
}
